/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @Original Author: jin Chen
 * @date: Apr 20, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1.config.model;

import edu.psu.geovista.common.utils.collection.CollectionUtils;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ModelUtils {
    /*******************************************************************************************************
     *                selected attributes
     *******************************************************************************************************/
    public static String[] getSelectAttributeNames(VariableData data){
        // Attributes attrs=data.getSelectedAttributes() ;
         List attlist=data.getSelectedVariableNames() ;

         if(attlist.size() <=0) return new String [0];

        String[] names = CollectionUtils.convert2StringArrayNoNull(attlist);

        return names;

    }

    public static void setSelectAttributeNames(VariableData data, String [] attrnames){
         //Attributes attrs=data.getSelectedAttributes() ;
         List attlist=data.getSelectedVariableNames() ;
         attlist.clear() ;//not keep previously selected attributes

         List list = CollectionUtils.convertArrayToList(attrnames);
         data.setSelectedVariableNames(list);

     }

    /*******************************************************************************************************
     *                paths
     *******************************************************************************************************/

    public static List getPaths (VariableData data){
        //Paths pathtype=data.getPaths() ;
        return data.getPaths();
        /*List pathList=data.getPaths() ;
        Properties paths=new Properties();
        //String [] paths=getStringArrayForList(pathList);
        for (int i=0;i<pathList.size() ;i++){
            Path apath=(Path) pathList.get(i);
            String name=apath.getName() ;
            String value=apath.getValue() ;
            paths.put(name,value);
        }
        return paths;*/
    }
    

    public static void setPcpPaths(Project project,Hashtable paths){
         Pcpdata pcpdata=project.getPcpdata() ;
         setPaths(pcpdata,paths);
     }

    public static void setTsPaths(Project project,Hashtable paths){
         TimeSeriesData tsdata=project.getTsdata() ;
         setPaths(tsdata,paths);
     }
    public static void setPcpPaths(Project project,List<Path> paths){
         Pcpdata pcpdata=project.getPcpdata() ;
         setPaths(pcpdata,paths);
     }

    public static void setTsPaths(Project project,List<Path> paths){
         TimeSeriesData tsdata=project.getTsdata() ;
         setPaths(tsdata,paths);
     }

    public static void setPaths(VariableData data,Hashtable paths){
       /* Properties mypaths = data.getPaths();
        mypaths.putAll(paths);*/
        List pathList=data.getPaths() ;
        pathList.clear() ;//to avoid duplicate data
        Set names=paths.keySet();
        for (Iterator iterator = names.iterator(); iterator.hasNext();) {
            String name = (String) iterator.next();
            String value=(String) paths.get(name);
            Path apath=new DfPath();
            apath.setName(name);
            apath.setValue(value);
            pathList.add(apath);
        }
    }
     public static void setPaths(VariableData data,List<Path> paths){
       /* Properties mypaths = data.getPaths();
        mypaths.putAll(paths);*/
        List pathList=data.getPaths() ;
        pathList.clear() ;//to avoid duplicate data
        pathList.addAll(paths);
        /*Set names=paths.keySet();
        for (Iterator iterator = names.iterator(); iterator.hasNext();) {
            String name = (String) iterator.next();
            String value=(String) paths.get(name);
            Path apath=new DfPath();
            apath.setName(name);
            apath.setValue(value);
            pathList.add(apath);
        }*/
    }

    /*******************************************************************************************************
     *                 Time values
     *******************************************************************************************************/
     /* public static Properties getTimeValues(Tsdata tsdata){
        return tsdata.getTimeValues();

        /*List list=tsdata.getTimeValues() ;
        Properties tvs=new Properties();
        for (int i=0;i<tvs.size();i++){
            TimeValue tv =(TimeValue) list.get(i);
            tvs.put(tv.getVarname() ,new Float(tv.getValue()));
        }
        return tvs;
    }*/

   /*public static void setTimeValues(Tsdata tsdata,  Hashtable tvs){
        //Tvs timevalue= tsdata.getTimeValues();
        List tvs=tsdata.getTimeValues() ;
        //list.clear() ;
        Enumeration keys=tvs.keys();
        while (keys.hasMoreElements()) {
            String name = (String) keys.nextElement();
            Integer value=(Integer) tvs.get(name);
            //TvImpl tv=getTvtypeImplByName(timevalue,name);
            TimeValue tv=getTimeValue()
            if(tv!=null){

                tv.setValue(value.intValue() );
                //list.add(tv);
            }
            else{
                tv=new TvImpl();
                tv.setAttrName(name);
                tv.setValue(value.intValue());
                list.add(tv);
            }

        }
        //System.out.println("");
    }*/
  /*  public static TimeValue getTimeValue(List <TimeValue> tvs, String vname){
        for (Iterator iterator = tvs.iterator(); iterator.hasNext();) {
            TimeValue timeValue = (TimeValue) iterator.next();
            if(timeValue.getVarname().equals(vname) )
                return timeValue;
        }
        return null;


    }*/

    /*******************************************************************************************************
       *                projected shape data
       *******************************************************************************************************/
/*
    public static boolean isDataProjected(Pcpdata pcpdata ) {
        Shape shp = pcpdata.getShape();
        return shp.isProjected() ;
    }*/

    /*public static void setDataProjected(Pcpdata pcpdata, boolean projected ) {
        Shape shp=pcpdata.getShape();
        shp.setProjected(projected);
    }*/

    /**
     *
     * @param list
     * @return  a String[] with same len as list
     */
    private static String[] getStringArrayForList(List list){
          if(list.size() <=0) return new String [0];

         String [] array=new String[list.size()];
         return  array;
    }

    /**
     *
     * @param list
     * @return  a String[] with same len as list
     */
    private static int[] getIntArrayForList(List list){
          if(list.size() <=0) return new int[0];

         int [] array=new int[list.size()];
         return  array;
    }

    /**
     *  
     *  
     * @return a project element with no null sub elements
     */
    public static Project getProjectInstance(){
        /* 
        */

         Project proj=new DfPathInfo();
         return proj;
    }

    //deep copy
    public static void  copyProperties(Project frm, Project to){
        /*try {
            BeanUtils.copyProperties(frm,to);
        } catch (IllegalAccessException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InvocationTargetException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }*/
        Pcpdata pcpdata=frm.getPcpdata() ;
        TimeSeriesData tsdata=frm.getTsdata() ;

        setPcpPaths(to,getPaths(pcpdata));
        setTsPaths(to,getPaths(tsdata));

        setSelectAttributeNames(to.getPcpdata() ,getSelectAttributeNames(pcpdata) );
        setSelectAttributeNames(to.getTsdata() ,getSelectAttributeNames(tsdata) );

        to.getPcpdata().setShapeProjected(pcpdata.isShapeProjected() );
        to.getPcpdata().setLoad(pcpdata.isLoad() );
        to.getTsdata().setLoad(tsdata.isLoad() );


    }

    //deep copy
    public static void  copyObject(Project frm, Project to){
        copyProperties(frm,to);
        to.setTime(frm.getTime() );
        to.setID(frm.getID());
    }
}
